﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Configuration;

namespace NCKH.Admin
{
    public partial class Loaihinh : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                hienloaihinh();
                // hienloaihinh();
            }
        }
        protected bool kiemtraloaihinh()
        {
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            string selectLoaihinh = "Select TenLH from LOAIHINH where TenLH='" + txttenLH.Text + "'";
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(selectLoaihinh, cnn))
                {
                    cmd.CommandType = CommandType.Text;
                    cnn.Open();
                    using (SqlDataReader rd = cmd.ExecuteReader())
                    {
                        if (rd.Read())
                        {
                            Response.Write("Đã tồn tại LH!");
                            return false;
                        }
                        else
                        {
                            Response.Write("Thêm thành công!");
                            return true;
                        }
                        rd.Close();
                    }
                    cnn.Close();
                }
            }
        }
        protected void ibtThem_Click(object sender, EventArgs e)
        {
            if (kiemtraloaihinh())
            {
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection cnn = new SqlConnection(connectionString))
                using (SqlCommand cmd = new SqlCommand("Themloaihinh", cnn))
                {
                    cmd.CommandType = CommandType.StoredProcedure;
                    cmd.Parameters.AddWithValue("@TenLH", txttenLH.Text);
                    cnn.Open();
                    cmd.ExecuteNonQuery();
                    cnn.Close();

                }

            }
            hienloaihinh();
        }
        protected void hienloaihinh()
        {
            using (DataTable LOAIHINH = getloaihinh())
            {
                grvloaihinh.DataSource = LOAIHINH;
                grvloaihinh.DataBind();
            }
        }
        DataTable getloaihinh()
        {
            string sqlSelect = "SELECT MaLH,TenLH  FROM LOAIHINH";
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand cmd = new SqlCommand(sqlSelect, cnn))
                {
                    using (SqlDataAdapter da = new SqlDataAdapter(cmd))
                    {
                        DataTable tbl = new DataTable("LOAIHINH");
                        da.Fill(tbl);
                        return tbl;
                    }
                }
            }
        }


        protected void grvloaihinh_RowCommand(object sender, GridViewCommandEventArgs e)
        {
            if (e.CommandName.ToLower().Equals("xoa"))
            {
                int loaihinhID = Convert.ToInt32(e.CommandArgument);
                string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
                using (SqlConnection Cnn = new SqlConnection(connectionString))
                {
                    using (SqlCommand Cmd = new SqlCommand("Xoaloaihinh", Cnn))
                    {
                        Cmd.CommandType = CommandType.StoredProcedure;
                        Cmd.Parameters.AddWithValue("@MaLH", loaihinhID);
                        Cnn.Open();
                        Cmd.ExecuteNonQuery();
                        Cnn.Close();
                        hienloaihinh();
                    }
                }
            }
        }
        protected void delete_loaihinh(int lhinhID)
        {
            // if (e.CommandName.ToLower().Equals("xoa"))
            //{
            // int chuyenmucID = Convert.ToInt32(e.CommandArgument);
            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Xoaloaihinh", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@MaLH", lhinhID);
                    Cnn.Open();
                    Cmd.ExecuteNonQuery();
                    Cnn.Close();

                }
            }
            // }
        }
        protected void ibtXoa_Click(object sender, EventArgs e)
        {
            foreach (GridViewRow r in grvloaihinh.Rows)
            {
                CheckBox cb = r.FindControl("chkchon") as CheckBox;
                if (cb != null && cb.Checked)
                {
                    int lhinhID = Convert.ToInt32(grvloaihinh.DataKeys[r.RowIndex].Value);
                    delete_loaihinh(lhinhID);
                }
            }
            hienloaihinh();
        }
        protected void ibtHuy_Click(object sender, EventArgs e)
        {
            txttenLH.Text =
            string.Empty;
            txttenLH.Focus();
        }
        protected void ibtTimkiem_Click(object sender, EventArgs e)
        {

            using (DataTable LOAIHINH = hienthongtintimkiem())
            {
                grvloaihinh.DataSource = LOAIHINH;
                grvloaihinh.DataBind();
            }

        }
        private DataTable hienthongtintimkiem()
        {

            string connectionString = ConfigurationManager.ConnectionStrings["NCKH"].ConnectionString;
            using (SqlConnection Cnn = new SqlConnection(connectionString))
            {
                using (SqlCommand Cmd = new SqlCommand("Timloaihinh", Cnn))
                {
                    Cmd.CommandType = CommandType.StoredProcedure;
                    Cmd.Parameters.AddWithValue("@TenLH", '%' + txtTimkiem.Text + '%');
                    using (SqlDataAdapter da = new SqlDataAdapter(Cmd))
                    {
                        da.SelectCommand = Cmd;
                        DataTable tb = new DataTable("LOAIHINH");
                        da.Fill(tb);
                        return tb;
                    }
                }
            }
        }
    }
}